using System;
using System.Collections;
using System.Data;
using System.Web;

using CE.Database;
/// <summary>
/// Summary description for UserProfileDB
/// </summary>
/// 
namespace CE.User
{
    public class UserViewHistoryDB
    {
        public UserViewHistoryDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 LogViewedHistory(string PageType, HttpRequest ClientRequest, string Description)
        {
            UserViewHistory _userview = new UserViewHistory();
            //UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
            string _ipNumber = ClientRequest.UserHostAddress;
            string _browsertype = ClientRequest.Browser.Type;
            string _pageType = PageType;
            string _viewDate = DateTime.Now.ToString("yyyyMMdd");
            string _browserInfo = "";
            Int64 _retViewedDetailId = 0;

            _browserInfo += "<UserBrowser>";
            _browserInfo += "<Browser>" + ClientRequest.Browser.Browser + "</Browser>";
            _browserInfo += "<Type>" + ClientRequest.Browser.Type + "</Type>";
            _browserInfo += "<Version>" + ClientRequest.Browser.Version + "</Version>";
            _browserInfo += "<MajorVersion>" + ClientRequest.Browser.MajorVersion.ToString() + "</MajorVersion>";
            _browserInfo += "<MinorVersionString>" + ClientRequest.Browser.MinorVersionString + "</MinorVersionString>";

            _browserInfo += "<JavaScript>" + ClientRequest.Browser.JavaScript.ToString() + "</JavaScript>";
            _browserInfo += "<VBScript>" + ClientRequest.Browser.VBScript.ToString() + "</VBScript>";
            _browserInfo += "<ActiveXControls>" + ClientRequest.Browser.ActiveXControls.ToString() + "</ActiveXControls>";
            _browserInfo += "<Frames>" + ClientRequest.Browser.Frames.ToString() + "</Frames>";
            _browserInfo += "<Tables>" + ClientRequest.Browser.Tables.ToString() + "</Tables>";
            _browserInfo += "<Cookies>" + ClientRequest.Browser.Cookies.ToString() + "</Cookies>";
            _browserInfo += "<ClrVersion>" + ClientRequest.Browser.ClrVersion.ToString() + "</ClrVersion>";
            _browserInfo += "<JavaApplets>" + ClientRequest.Browser.JavaApplets.ToString() + "</JavaApplets>";

            _browserInfo += "<Win16>" + ClientRequest.Browser.Win16.ToString() + "</Win16>";
            _browserInfo += "<Win32>" + ClientRequest.Browser.Win32.ToString() + "</Win32>";
            _browserInfo += "<Platform>" + ClientRequest.Browser.Platform + "</Platform>";
            _browserInfo += "<UserAgent>" + ClientRequest.UserAgent + "</UserAgent>";
            _browserInfo += "<UserHostAddress>" + ClientRequest.UserHostAddress + "</UserHostAddress>";
            _browserInfo += "<UserHostName>" + ClientRequest.UserHostName + "</UserHostName>";

            _browserInfo += "<IsSecureConnection>" + ClientRequest.IsSecureConnection.ToString() + "</IsSecureConnection>";
            _browserInfo += "<AnonymousID>" + ClientRequest.AnonymousID + "</AnonymousID>";
            _browserInfo += "<ContentLength>" + ClientRequest.ContentLength.ToString() + "</ContentLength>";
            _browserInfo += "<ContentType>" + ClientRequest.ContentType.ToString() + "</ContentType>";
            _browserInfo += "<HttpMethod>" + ClientRequest.HttpMethod + "</HttpMethod>";
            _browserInfo += "</UserBrowser>";

            _userview = GetUserViewHistory(_ipNumber, _pageType, _browsertype, _viewDate);
            if (_userview == null)
            {
                _userview = new UserViewHistory();
                _userview.IPNumber = _ipNumber;
                _userview.PageType = _pageType;
                _userview.BrowserType = _browsertype;
                _userview.BrowserInfo = _browserInfo;
                _userview.ViewedCount = 1;
                _userview.ViewedDescription = Description;
                _retViewedDetailId = Insert(_userview);
            }
            else
            {
                //_userview.BrowserInfo = _browserInfo;
                _userview.ViewedCount += 1;
                _userview.ViewedDescription = Description;
                _retViewedDetailId = Insert(_userview);
            }
            _userview = null;
            return _retViewedDetailId;
        }

        public Int64 Insert(UserViewHistory userview)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocUserViewedHistoryInsertUpdateSingleItem]";
            
            SortedList _sPara = new SortedList();
            
            #region Parameter
                _sPara.Add("@ViewedId", userview.ViewedId);
                _sPara.Add("@IPNumber", userview.IPNumber);
                _sPara.Add("@PageType", userview.PageType);
                _sPara.Add("@BrowserType", userview.BrowserType);
                _sPara.Add("@BrowserInfo", userview.BrowserInfo);
                _sPara.Add("@ViewedCount", userview.ViewedCount);
                _sPara.Add("@ViewedDescription", userview.ViewedDescription);
                _sPara.Add("@Info1", userview.Info1);
                _sPara.Add("@Info2", userview.Info2);
                _sPara.Add("@Info3", userview.Info3);
                _sPara.Add("@Info4", userview.Info4);
                _sPara.Add("@Info5", userview.Info5);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public UserViewHistoryList GetUserViewHistoryListByViewedDate(string ViewedDate)
        {
            UserViewHistoryList _myviewhistorylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserViewedHistorySelectItemListByViewedDate";

            SortedList _spara = new SortedList();
            _spara.Add("@ViewedDate", ViewedDate);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _myviewhistorylist = new UserViewHistoryList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        UserViewHistory _myviewhistory = new UserViewHistory();
                        _myviewhistory = FillDataRecord(_dt, i);
                        _myviewhistorylist.Add(_myviewhistory);
                    }
                }
            }
            return _myviewhistorylist;
        }

        public UserViewHistoryList GetUserViewHistoryListByViewedDateIPNumber(string ViewedDate, string IPNumber)
        {
            UserViewHistoryList _myviewhistorylist = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserViewedHistorySelectItemListByDateIPNumber";

            SortedList _spara = new SortedList();
            _spara.Add("@ViewedDate", ViewedDate);
            _spara.Add("@IPNumber", IPNumber);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _myviewhistorylist = new UserViewHistoryList();
                    for (int i = 0; i < _dt.Rows.Count; i++)
                    {
                        UserViewHistory _myviewhistory = new UserViewHistory();
                        _myviewhistory = FillDataRecord(_dt, i);
                        _myviewhistorylist.Add(_myviewhistory);
                    }
                }
            }
            return _myviewhistorylist;
        }

        public UserViewHistory GetUserViewHistory(string IPNumber, string PageType, string BrowserType, string ViewedDate)
        {
            UserViewHistory _viewhistory = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocUserViewedHistorySelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@IPNumber", IPNumber);
            _spara.Add("@PageType", PageType);
            _spara.Add("@BrowserType", BrowserType);
            _spara.Add("@ViewedDate", ViewedDate);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _viewhistory = new UserViewHistory();
                    _viewhistory = FillDataRecord(_dt, 0);
                }
            }
            return _viewhistory;
        }

        private UserViewHistory FillDataRecord(DataTable myDataTable, int RowId)
        {
            UserViewHistory myUserView = new UserViewHistory();
            myUserView.ViewedId = Convert.ToInt64(myDataTable.Rows[RowId]["ViewedId"].ToString());
            myUserView.IPNumber = myDataTable.Rows[RowId]["IPNumber"].ToString();
            myUserView.PageType = myDataTable.Rows[RowId]["PageType"].ToString();
            myUserView.BrowserType = myDataTable.Rows[RowId]["BrowserType"].ToString();
            myUserView.BrowserInfo = myDataTable.Rows[RowId]["BrowserInfo"].ToString();
            myUserView.ViewedCount = Convert.ToByte(myDataTable.Rows[RowId]["ViewedCount"].ToString());
            //myUserView.ViewedDescription = myDataTable.Rows[RowId]["ViwedDescription"].ToString();
            myUserView.ViewedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ViewedDate"].ToString());
            myUserView.Info1 = myDataTable.Rows[RowId]["Info1"].ToString();
            myUserView.Info2 = myDataTable.Rows[RowId]["Info2"].ToString();
            myUserView.Info3 = myDataTable.Rows[RowId]["Info3"].ToString();
            myUserView.Info4 = myDataTable.Rows[RowId]["Info4"].ToString();
            myUserView.Info5 = myDataTable.Rows[RowId]["Info5"].ToString();
            return myUserView;
        }

        public Int64 GetViewedId(HttpRequest ClientRequest)
        {
            Int64 _viewedId = 0;
            string _ipNumber = ClientRequest.UserHostAddress;
            string _browsertype = ClientRequest.Browser.Type;
            string _pageType = "CC";
            string _viewDate = DateTime.Now.ToString("yyyyMMdd");
            UserViewHistory _userview = new UserViewHistory();
            UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
            _userview = _userviewdb.GetUserViewHistory(_ipNumber, _pageType, _browsertype, _viewDate);
            if (_userview != null)
            {
                _viewedId = Convert.ToInt64(_userview.ViewedId);
            }
            return _viewedId;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete()
        {
            return true;
        }
        
    }
}